// @ts-ignore
import Hierarchy from '@antv/hierarchy';
// @ts-ignore
import { Markup, Model } from '@antv/x6';


interface HierarchyResult {
  id: number
  x: number
  y: number
  data: any
  children?: HierarchyResult[]
}

export const getMindmapData = (data: any,currentItem:any) => {
  const result = Hierarchy.mindmap(data, {
    direction: 'H',
    getHeight() {
      return 91
    },
    getWidth() {
      return 303
    },
    getHGap() {
      return 40
    },
    getVGap() {
      return 20
    },
    getSide: () => {
      return 'right'
    },
  })
  return result;
}

//,onAddSymbolCard: (data: any) => void
export const getIndexCells = (
    data: any, 
    onRemoveEvent: (data: any) => void, 
    onAddIndexCard: (data: any, test:any) => void, 
    clickMetricsCard:(data: any) => void,
    onPopoverDelete:(data: any) => void,
    currentItem?:any
  ) => {
  const model: Model.FromJSONData = { nodes: [], edges: [] }
  const traverse = (data: HierarchyResult) => {
    if (data.data) {
      let node: any = data;
      let xWidth = 0;
      if(model && model.nodes && model.nodes.length>0 && node.parent){
        let nodeData:any = model.nodes.find(item=>item.id == node.parent.id);
        if(nodeData && nodeData.data.type === 'indexCard'){
          xWidth = nodeData.x + 383;
        }else{
          xWidth = nodeData.x + 200;
        }
        
      }
      model.nodes?.push({
        id: `${data.id}`,
        x: xWidth,
        y: data.data.type === 'indexCard' ? data.y : data.y + 26.5,
        zIndex: data.data.type === 'symbolCard' ? 10 : 1,
        shape: data.data.type === 'indexCard' ? 'metricsNode' : 'symbolNode',
        width: data.data.type === 'indexCard' ? 303 : 38,
        height:  data.data.type === 'indexCard' ? 91 : 38,
        data: {...data.data, onRemoveEvent, onAddIndexCard, clickMetricsCard,onPopoverDelete},
      })
    }
    if (data.children) {
      data.children.forEach((item: HierarchyResult) => {
        model.edges?.push({
          shape: 'bpmn-edge',
          source: {
            cell: `${data.id}`,
            anchor:
            {
              name: 'right',
              args: {
                dx: -16,
              },
            },

          },
          target: {
            cell: `${item.id}`,
            anchor:
            {
              name: 'left',
              // args: {
              //   dx: -16,
              // },
            },
          },
          router: {
            name: 'er',
            args: {
              offset: 'center',
              direction: 'H'
            },
          },
          connector: {
            name: 'rounded',
            args: {
              radius: 6,
            },
          },
          data: {
            source: data.data,
            target: item.data
          },
          
          defaultLabel: {
            markup: Markup.getForeignObjectMarkup(),
            attrs: {
              fo: {
                width: 16,
                height: 16,
                x: -27,
                y: -10,
              },
            },
          },
          label: {
            position: 1,
          },

        })
        traverse(item)
      })
    }
  }
  const result = getMindmapData(data, currentItem);
  console.log(result)
  traverse(result);

  return model;
}
export const mapAllNode = (data:any, id:any) => {
  data.heightLightLine = false
  if (data.id === id) {
    data.heightLightLine = true
  }
  const {children} = data
  if (children) {
    for(let i = 0, len = children.length; i < len; i++) {
      children[i].heightLightLine = false
      if (children[i].children) {
        mapAllNode(children[i], id)
      }
    }
  }
  return data
}

export const clearOtherField = (data:any) => {
  delete data.heightLightLine
  const {children} = data
  if (children) {
    for(let i = 0, len = children.length; i < len; i++) {
      delete children[i].heightLightLine
      if (children[i].children) {
        clearOtherField(children[i])
      }
    }
  }
  return data
}
const loopTree = (data:any) => {
  if (data?.children?.length) {
    const { children } = data
    for (let i = 0, len = children.length; i < len; i++) {
      children[i].heightLightLine = false
      loopTree(children[i])
    }
  }
}
export const findItem = (
  obj: any,
  id: string | number,
  type?: string
): {
  parent: any | null
  node: any | null
} | null => {
  if (obj.id == id) {
    if (obj.type==='symbolCard') {
      obj.heightLightLine = false
    } 
    loopTree(obj)
    return {
      parent: null,
      node: obj,
    }
  }
  const { children } = obj
  obj.heightLightLine = false
  if (children) {
    for (let i = 0, len = children.length; i < len; i++) {
      const res = findItem(children[i], id)
      if (type === 'addMetricsNode') {
        children[i].heightLightLine = false
        loopTree(children[i])
      }
      if (res) {
        obj.heightLightLine = false
        return {
          parent: res.parent || obj,
          node: res.node,
        }
      }
    }
  }
  return null
}
export const removeNode = (data:any,id: string) => {
  const res = findItem(data, id)
  const dataItem = res?.parent
  if (dataItem && dataItem.children) {
    const { children } = dataItem
    const index = children.findIndex((item:any) => item.id === id)
    return children.splice(index, 1)
  }
  return null
}

export const replaceNode = (data:any,replaceData:any, id: string) => {
  const res = findItem(data, id)
  const dataItem = res?.parent
  replaceData.parentId = data.parentId
  if (dataItem && dataItem.children) {
    const { children } = dataItem
    const index = children.findIndex((item:any) => item.id === id)
    replaceData.children = children[index]?.children

    Object.keys(children[index]).forEach(key=>{
      replaceData[key] = replaceData.hasOwnProperty(key) ? replaceData[key] : children[index][key]
    })
    return children.splice(index, 1, replaceData)
  }
  replaceData.children = data.children
  replaceData.children = replaceData.children.map((v:any) => {
    return {
      ...v,
      parentId: replaceData.id
    } 
  })
  Object.keys(data).forEach(key=>{
    data[key] = replaceData.hasOwnProperty(key) ? replaceData[key] : data[key] 
  })
  return data
}
export const addChildNode = (data:any, item: any, id: string, type: string) => {
  const res = findItem(data, id, type)
  const dataItem = res?.node
  if (dataItem) {
    if (item) {
      item.parentId = id
      if (dataItem.children) {
        dataItem.children.push(item)
      } else {
        dataItem.children = [item]
      }
      return item
    }
  }
  return null
}
